#include <iostream>
#include <unistd.h>
#include "Comm.hpp"

int main()
{
    // 1. 创建 key
    key_t k = ::ftok(gpath.c_str(), gprojID);
    if(k < 0)
    {
        std::cerr << "fork error" << std::endl;
        return 1;
    }
    std::cout << "k : " << ToHex(k) << std::endl;

    // 2. 创建共享内存 && 获取
    // 注意：共享内存也有权限！
    int shmid = ::shmget(k, gshmsize, IPC_CREAT | IPC_EXCL | gmode);
    if(shmid < 0)
    {
        std::cout << "shmget error" << std::endl;
        return 2;
    }
    std::cout << "shmid : " << shmid << std::endl;

    sleep(5);

    // 3. 共享内存挂接到自己的地址空间中
    void *ret = shmat(shmid, nullptr, 0);
    std::cout << "attach done:" << (long long)ret << std::endl;

    sleep(5);


    // n. 删除共享内存
    shmctl(shmid, IPC_RMID, nullptr);
    std::cout << "delete shm done" << std::endl;

    sleep(5);


    return 0;
}